using System;
using System.Collections.Generic;
using System.Text;
using Mpi.Common;

namespace Xeml.Visualization
{
    public class TimeRegionList : LinkedList<TimeRegion>
    {
        #region Fields
        private bool changeEventSuspended = false;
        #endregion

        #region Properties

        /// <summary>
        /// The all-over length of the time rgion collection.
        /// </summary>
        public TimeSpan Length
        {
            get
            {
                TimeSpan max = TimeSpan.Zero;
                foreach (TimeRegion tr in this)
                {
                    max += tr.Length;
                }
                return max;
            }
        }

        public TimeRegion this[int idx]
        {
            get
            {
                if (idx < 0 || idx > this.Count - 1)
                    throw new IndexOutOfRangeException();

                LinkedListNode<TimeRegion> t = this.First;
                if (idx > 0)
                {
                    for (int i = 1; i <= idx; i++)
                    {
                        t = t.Next;
                    }
                }
                return t.Value;
            }
        }
        #endregion

        #region Constructors

        /// <summary>
        /// The standard constructor
        /// </summary>
        public TimeRegionList()
            : base()
        {

        }
        #endregion

        #region Methods
        /// <summary>
        /// Adds a time region at the beginning of the list.
        /// </summary>
        /// <param name="item">The item that will be added.</param>
        public new void AddFirst(TimeRegion item)
        {
            base.AddFirst(item);
            item._collection = this;
            item.ResolutionChanged += new EventHandler<ResolutionEventArgs>(OnItemResolutionChanged);
            item.LengthHasChanged += new EventHandler<Mpi.Common.PropertyHasChangedEventArgs<TimeSpan>>(OnItemLengthChanged);
            OnChanged();
        }

      

        /// <summary>
        /// Adds a time region at the end of the list.
        /// </summary>
        /// <param name="item">The item that will be added.</param>
        public new void AddLast(TimeRegion item)
        {
            base.AddLast(item);
            item._collection = this;
            item.ResolutionChanged += new EventHandler<ResolutionEventArgs>(OnItemResolutionChanged);
            item.LengthHasChanged += new EventHandler<Mpi.Common.PropertyHasChangedEventArgs<TimeSpan>>(OnItemLengthChanged);
            OnChanged();
        }

        public new void AddBefore(LinkedListNode<TimeRegion> node, TimeRegion val)
        {
            base.AddBefore(node, val);
            val.Collection = this;
            val.ResolutionChanged += new EventHandler<ResolutionEventArgs>(OnItemResolutionChanged);
            val.LengthHasChanged += new EventHandler<Mpi.Common.PropertyHasChangedEventArgs<TimeSpan>>(OnItemLengthChanged);
            OnChanged();
        }

        public new void AddBefore(LinkedListNode<TimeRegion> node, LinkedListNode<TimeRegion> newnode)
        {
            base.AddBefore(node, newnode);
            newnode.Value.Collection = this;
            newnode.Value.ResolutionChanged += new EventHandler<ResolutionEventArgs>(OnItemResolutionChanged);
            newnode.Value.LengthHasChanged += new EventHandler<Mpi.Common.PropertyHasChangedEventArgs<TimeSpan>>(OnItemLengthChanged);
            OnChanged();
        }

        public new void AddAfter(LinkedListNode<TimeRegion> node, TimeRegion val)
        {
            base.AddAfter(node, val);
            val.Collection = this;
            val.ResolutionChanged += new EventHandler<ResolutionEventArgs>(OnItemResolutionChanged);
            val.LengthHasChanged += new EventHandler<Mpi.Common.PropertyHasChangedEventArgs<TimeSpan>>(OnItemLengthChanged);
            OnChanged();
        }

        public new void AddAfter(LinkedListNode<TimeRegion> node, LinkedListNode<TimeRegion> newnode)
        {
            base.AddAfter(node, newnode);
            newnode.Value.Collection = this;
            newnode.Value.ResolutionChanged += new EventHandler<ResolutionEventArgs>(OnItemResolutionChanged);
            newnode.Value.LengthHasChanged += new EventHandler<Mpi.Common.PropertyHasChangedEventArgs<TimeSpan>>(OnItemLengthChanged);
            OnChanged();
        }

        /// <summary>
        /// Removes an item.
        /// </summary>
        /// <param name="item">The item that will be removed.</param>
        public new void Remove(TimeRegion item)
        {
            base.Remove(item);
            item._collection = null;
            item.ResolutionChanged -= new EventHandler<ResolutionEventArgs>(OnItemResolutionChanged);
            item.LengthHasChanged -= new EventHandler<Mpi.Common.PropertyHasChangedEventArgs<TimeSpan>>(OnItemLengthChanged);
            //if (RegionRemoved != null)
            //{
            //    RegionRemoved(this, new RegionRemovedEventArgs(item));
            //}
            OnChanged();
        }

        /// <summary>
        /// Removes all items.
        /// </summary>
        public new void Clear()
        {
            foreach (TimeRegion tr in this)
            {
                tr.ResolutionChanged -= new EventHandler<ResolutionEventArgs>(OnItemResolutionChanged);
                tr.LengthHasChanged -= new EventHandler<Mpi.Common.PropertyHasChangedEventArgs<TimeSpan>>(OnItemLengthChanged);
            }
            base.Clear();
            OnChanged();
        }

        /// <summary>
        /// Calls all assigned changed event handler.
        /// </summary>
        internal virtual void OnChanged()
        {
            if (Changed != null && !changeEventSuspended)
                Changed(this, null);
        }

        /// <summary>
        /// Calculates the start pixel of a specific time region.
        /// </summary>
        /// <param name="tr">The region.</param>
        /// <returns>The calculated pixel.</returns>
        public long StartPxOf(TimeRegion tr)
        {
            LinkedListNode<TimeRegion> lln = this.Find(tr);
            long ret = 0;
            while (lln.Previous != null)
            {
                lln = lln.Previous;
                ret += lln.Value.PxLength;
            }

            return ret;
        }

        /// <summary>
        /// Inhibit all alls changed events.
        /// </summary>
        public void SuspendChangeEvent()
        {
            changeEventSuspended = true;
        }

        /// <summary>
        /// Resumes the firing off changed events.
        /// </summary>
        /// <param name="fireNow"></param>
        public void ResumeChangeEvent(bool fireNow)
        {
            changeEventSuspended = false;
            if (fireNow)
                OnChanged();
        }
        #endregion

        #region Events-/handling
        void OnItemResolutionChanged(object sender, ResolutionEventArgs e)
        {
            if (e.NewValue != e.OldValue)
            {
                OnChanged();
            }
        }

        void OnItemLengthChanged(object sender, Mpi.Common.PropertyHasChangedEventArgs<TimeSpan> e)
        {
            if (this.LengthChanged != null)
            {
                LengthChanged(this,new PropertyHasChangedEventArgs<TimeSpan>(this.Length + (e.NewValue-e.OldValue),this.Length));
            }
        }

        /// <summary>
        /// An event handler if something changed.
        /// </summary>
        internal event EventHandler Changed;
        public event EventHandler<RegionRemovedEventArgs> RegionRemoved;
        public event EventHandler<PropertyHasChangedEventArgs<TimeSpan>> LengthChanged;

        #endregion
    }


    public class RegionRemovedEventArgs : EventArgs
    {
        private TimeRegion tr;
        public TimeRegion TimeRegion
        {
            get { return tr; }
        }

        public RegionRemovedEventArgs(TimeRegion tr)
        {
            this.tr = tr;
        }
    }
}
